package com.simon.batteryinformation;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.IntentFilter;
import android.os.BatteryManager;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import android.content.Intent;

public class BatteryInformationActivity extends Activity {
	TextView textBatteryLevel;

	private static final int SHOW_ID = Menu.FIRST;
	private String info = new String();

	private BatteryDBAdapter mDbHelper;

	/**
	 * Called when the activity is first created. Create database if not exists.
	 * Open connection to database.
	 **/
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		mDbHelper = new BatteryDBAdapter(this);
		mDbHelper.open();
		registerBatteryReceiver();

	}

	/**
	 * Create option in menu.
	 **/
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		menu.add(0, SHOW_ID, 0, R.string.showData);
		return true;
	}

	/**
	 * Adds action to options in menu. TODO action
	 **/
	@Override
	public boolean onMenuItemSelected(int featureId, MenuItem item) {
		switch (item.getItemId()) {
		case SHOW_ID:
			showData();
			return true;
		}

		return super.onMenuItemSelected(featureId, item);
	}

	/**
	 * Clean before activity destroy.
	 **/
	@Override
	protected void onDestroy() {
		mDbHelper.close();
		unregisterReceiver(batteryReceiver);
		super.onDestroy();
	}

	/**
	 * Run broadcast receiver.
	 **/
	private BroadcastReceiver batteryReceiver = new BroadcastReceiver() {
		public void onReceive(Context context, Intent intent) {
			String plugged;
			String health;
			String status;
			String volt;
			String temp;

			boolean rawisPresent = intent.getBooleanExtra("present", false);
			String rawtechnology = intent.getStringExtra("technology");
			int rawplugged = intent.getIntExtra("plugged", -1);
			int rawscale = intent.getIntExtra("scale", -1);
			int rawhealth = intent.getIntExtra("health", 0);
			int rawstatus = intent.getIntExtra("status", 0);
			int rawlevel = intent.getIntExtra("level", -1);
			int rawtemp = intent.getIntExtra("temperature", 0);
			int rawvolt = intent.getIntExtra("voltage", 0);
			int level = 0;

			Bundle bundle = intent.getExtras();

			Log.i("BatteryLevel", bundle.toString());

			if (rawisPresent) {
				if (rawlevel >= 0 && rawscale > 0) {
					level = (rawlevel * 100) / rawscale;
				}

				plugged = getPlugTypeString(rawplugged);
				health = getHealthString(rawhealth);
				status = getStatusString(rawstatus);
				volt = getVoltString(rawvolt);
				temp = getTemperature(rawtemp);

				setBatteryLevel(level);
				setTechnology(rawtechnology);
				setPlugged(plugged);
				setHealth(health);
				setStatus(status);
				setVoltage(volt);
				setTemperature(temp);

				mDbHelper.create(System.currentTimeMillis(), level, health,
						rawhealth, rawtechnology, plugged, rawplugged, status,
						rawstatus, volt, rawvolt, temp, rawtemp);

			} else {
				info = "Battery not present!!!";
				/**
				 * TODO - information displayed when the battery information is
				 * unavailable.
				 **/
				setInfo(info);
			}

		}
	};

	/**
	 * Run new activity that shows saved information about the battery. New
	 * activity BatteryDataShow.
	 */
	public void showData(View v) {

		Intent i = new Intent(this, BatteryDataShow.class);
		startActivity(i);
	}

	/**
	 * Run new activity that shows saved information about the battery. New
	 * activity BatteryDataShow.
	 */
	private void showData() {

		Intent i = new Intent(this, BatteryDataShow.class);
		startActivity(i);
	}

	/**
	 * Set the battery information.
	 * 
	 * @param info
	 *            Information in string about the battery.
	 **/
	private void setInfo(String info) {
		TextView textBatteryLevel = (TextView) findViewById(R.id.Information);
		textBatteryLevel.setText(info);
	}

	/**
	 * Set information about battery level
	 * 
	 * @param level
	 *            Information in integer about the battery level.
	 **/
	private void setBatteryLevel(int level) {
		TextView batteryLevel = (TextView) findViewById(R.id.BatteryLevel);
		batteryLevel.setText("Battery Level: " + level + "%");
	}

	/**
	 * Set information about that battery is plugged
	 * 
	 * @param Plugged
	 */
	private void setPlugged(String Plugged) {
		TextView plugged = (TextView) findViewById(R.id.Plugged);
		plugged.setText("Plugged: " + Plugged);

	}

	/**
	 * Set information about battery technology
	 * 
	 * @param Technology
	 */
	private void setTechnology(String Technology) {
		TextView technology = (TextView) findViewById(R.id.Technology);
		technology.setText("Technology: " + Technology);
	}

	/**
	 * Set information about battery health
	 * 
	 * @param Health
	 */
	private void setHealth(String Health) {
		TextView health = (TextView) findViewById(R.id.Health);
		health.setText("Health: " + Health);
	}

	/**
	 * Set information about battery Status
	 * 
	 * @param Status
	 */
	private void setStatus(String Status) {
		TextView status = (TextView) findViewById(R.id.Status);
		status.setText("Status: " + Status);
	}

	/**
	 * Set information about battery voltage
	 * 
	 * @param Voltage
	 */
	private void setVoltage(String Voltage) {
		TextView voltage = (TextView) findViewById(R.id.Voltage);
		voltage.setText("Voltage: " + Voltage);
	}

	/**
	 * Set information about battery temperature
	 * 
	 * @param Temperature
	 */
	private void setTemperature(String Temperature) {
		TextView temperature = (TextView) findViewById(R.id.Temperature);
		temperature.setText("Temperature: " + Temperature);
	}

	private String getTemperature(int temp) {
		return ((float) temp / 10) + " C";
	}

	private String getVoltString(int volt) {

		return ((float) volt / 1000) + " V";
	}

	private String getPlugTypeString(int plugged) {
		String plugType = "Unknown";

		switch (plugged) {
		case BatteryManager.BATTERY_PLUGGED_AC:
			plugType = "AC";
			break;
		case BatteryManager.BATTERY_PLUGGED_USB:
			plugType = "USB";
			break;
		}

		return plugType;
	}

	private String getHealthString(int health) {
		String healthString = "Unknown";

		switch (health) {
		case BatteryManager.BATTERY_HEALTH_DEAD:
			healthString = "Dead";
			break;
		case BatteryManager.BATTERY_HEALTH_GOOD:
			healthString = "Good";
			break;
		case BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE:
			healthString = "Over Voltage";
			break;
		case BatteryManager.BATTERY_HEALTH_OVERHEAT:
			healthString = "Over Heat";
			break;
		case BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE:
			healthString = "Failure";
			break;
		}

		return healthString;
	}

	private String getStatusString(int status) {
		String statusString = "Unknown";

		switch (status) {
		case BatteryManager.BATTERY_STATUS_CHARGING:
			statusString = "Charging";
			break;
		case BatteryManager.BATTERY_STATUS_DISCHARGING:
			statusString = "Discharging";
			break;
		case BatteryManager.BATTERY_STATUS_FULL:
			statusString = "Full";
			break;
		case BatteryManager.BATTERY_STATUS_NOT_CHARGING:
			statusString = "Not Charging";
			break;
		}

		return statusString;
	}

	private void registerBatteryReceiver() {
		IntentFilter filter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);

		registerReceiver(batteryReceiver, filter);
	}
}